﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using MixScriptWeb.Share;
using MixScript.BLL;
using MixScriptWeb.control;
using System.Web.UI.WebControls;
using MixScript.BLL.FI;
using System.Data;
using System.Xml;
using MixScriptWeb.Share;

namespace MixScriptWeb.Accounting
{
    public partial class fDailyExchange : cAccCommon, IMIxCommon
    {

        public static DataTable dt;
        public static SortDirection sd;
        public static AccMaster mn;
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                LoadData();
            }
        }
        private void LoadData()
        {
            ddlMonth.SelectedIndex = DateTime.Now.Month - 1;
            ddlYear.SelectedIndex = ((int)DateTime.Now.Year - 2011);
            FillDropDownList(ddlCurency, "mst_currency", "currency", "currency", true);
            FillListBox(lbCurrency, "mst_currency", "currency", "currency", true);
            lbCurrency_SelectedChange(null, null);
        }
        public bool isvalid()
        {
            return true;
        }


        protected void gvList_Sorting(object sender, GridViewSortEventArgs e)
        {
            if (sd == SortDirection.Ascending)
            {
                sd = SortDirection.Descending;
            }
            else
            {
                sd = SortDirection.Ascending;
            }

            DataView dataView = new DataView(dt);
            dataView.Sort = e.SortExpression + " " + ShortDirection(sd);

            bindGridview(gvList, dataView);
        }
        protected void gvList_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                Button btnE = e.Row.FindControl("lbEdit") as Button;
                Button btnD = e.Row.FindControl("lbDelete") as Button;
                ScriptManager.GetCurrent(this).RegisterAsyncPostBackControl(btnE);
                ScriptManager.GetCurrent(this).RegisterAsyncPostBackControl(btnD);
            }
        }
        protected void gvList_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {

            gvList.PageIndex = e.NewPageIndex;
            bindGridview(gvList, dt);

        }
        protected void gvList_RowCommand(Object sender, GridViewCommandEventArgs e)
        {

            Button lb = (Button)e.CommandSource;
            GridViewRow selectedRow = (GridViewRow)lb.NamingContainer;
            Int32 RowIndex = Convert.ToInt32(selectedRow.RowIndex);
            cCurrency oCur = new cCurrency();
            if (e.CommandName == "Editing")
            {

                string date = string.Format("{0:yyyy-MM-dd}", e.CommandArgument);
                TextBox txtR, txtB, txtS;

                txtR = (TextBox)gvList.Rows[RowIndex].FindControl("txtRate");
                txtB = (TextBox)gvList.Rows[RowIndex].FindControl("txtBuy");
                txtS = (TextBox)gvList.Rows[RowIndex].FindControl("txtSell");
                oCur.Update(lbCurrency.SelectedItem.Value.ToString(), date, txtR.Text, txtB.Text, txtS.Text);
                ShowStatusBar("Sukses", msgType.msgSuccess);
            }
            else if (e.CommandName == "Deleting")
            {
                string date = string.Format("{0:yyyy-MM-dd}", e.CommandArgument);
                oCur.Delete(lbCurrency.SelectedItem.Value.ToString(), date);
                ShowStatusBar("Sukses", msgType.msgSuccess);
            }
            lbCurrency_SelectedChange(null, null);

        }

        protected void lbCurrency_SelectedChange(Object sender, EventArgs e)
        {
            cCurrency oCur = new cCurrency();
            String Cur = "";
            if (lbCurrency.SelectedItem == null)
                Cur = lbCurrency.Items[0].Value.ToString();
            else
                Cur = lbCurrency.SelectedItem.Value.ToString();


            if (oCur.GetCurrency(out dt, ddlYear.SelectedItem.Value.ToString(), ddlMonth.SelectedItem.Value.ToString(), Cur))
            {


            }
            else
            {

            }
            gvList.DataSource = dt;
            gvList.DataBind();
        }
        protected void btnRefresh_Click(Object sender, EventArgs e)
        {
            lbCurrency_SelectedChange(null, null);
        }
        protected void btnSave_Click(Object sender, EventArgs e)
        {
            cCurrency oCur = new cCurrency();
            int less = 0;
            if (chkLess.Checked)
                less = 1;

            //oCur.Save(ddlCurency.SelectedItem.Value.ToString(), dateFrom.Text, dateTo.Text, txtRate.Text, txtBuy.Text, txtSell.Text,less);
            oCur.Save(ddlCurency.SelectedItem.Value.ToString(), dateFrom.Text, dateTo.Text, txtRate.Text, txtBuy.Text, txtSell.Text, chkLess.Checked);
            lbCurrency_SelectedChange(null, null);
        }
    }
}